#  /**
#  * Copyright (c) 2022 Beijing Jiaotong University
#  * PhotLab is licensed under [Open Source License].
#  * You can use this software according to the terms and conditions of the [Open Source License].
#  * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#  *
#  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#  *
#  * See the [Open Source License] for more details.
#  */
#  /**
#  * Author: Zheng Wang
#  * Created: Jul. 2, 2022
#  * Supported by: National Key Research and Development Program of China
#  */
from .port import Port
from phot_engine.globals import VerificationError


def _verify_instantiation_parameter(source_port, destination_port):
    """
        Verify two ports' validation
    :param source_port:
    :param destination_port:
    :return:
    """
    if isinstance(source_port, Port) and isinstance(destination_port, Port):
        return True
    else:
        return False


class Edge:
    """
        To describe an edge in the GUI
        edge can be imaged like port->port
    """

    def __init__(self, source_port: Port, destination_port: Port, signal_type, signal=None):
        """
            instantiate an Edge type
        :param source_port:
        :param destination_port:
        :param signal_type:
        :param signal:the calculated signal in an edge
        """
        if _verify_instantiation_parameter(source_port=source_port, destination_port=destination_port):
            self.source_port = source_port
            self.destination_port = destination_port
            self.signal_type = signal_type
            self.signal = signal
        else:
            raise VerificationError(
                "source_port,destination_port should be a port type,not {},{}".format(type(source_port),
                                                                                      type(destination_port)))

    def get_signal(self):
        return self.signal

    def __repr__(self):
        edge_info = "**********Edge Info**********\n" \
                    "source port:{}\n" \
                    "destination port:{}\n" \
                    "signal type:{}\n" \
                    "signal is None:{}\n" \
                    "********************\n".format(self.source_port,
                                                    self.destination_port,
                                                    self.signal_type,
                                                    (self.signal is None))

        return edge_info

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            if self.source_port == other.source_port \
                    and self.destination_port == other.destination_port \
                    and self.signal_type == other.signal_type:
                return True
            else:
                return False
        else:
            raise TypeError("unsupported operand type(s) for ==: {} and {}".format(type(self), type(other)))
